<?php
namespace app\index\controller;
use app\index\controller\Base;
use app\common\util\TreeUtil;
use \think\Db;


class Index extends Base
{
	public function _initialize(){
		parent::_initialize();
		
		$nav = Db::name('nav')->where(['isShow'=>1])->order('sorts',' asc')->select();
		$nav = TreeUtil::listToTreeMulti( $nav ,  0 , 'id' , 'pid' , 'child');
		$this->assign('nav',$nav);
		
		$confSite = Db::name('config')->where('confName','site_config')->value('confValue');
		$this->assign('confSite',json_decode($confSite,true));
		
		
	}
	
	
	
	/**
	 * 首页
	 */
    public function index()
    {
    	
    	return $this->fetch();
    }
    
    /**
     * 列表
     */
    public function lists(){
    
    	$cid = input('cid',0,'intval');
    	$this->assign('cid',$cid);
    	
    	$info = Db::name('column')->where('id',$cid)->find();
    	$this->assign('info',$info);
    	return $this->fetch();
    }
    
    
    /**
     * 详情
     */
    public function detail(){
    	$cid = input('cid',0,'intval');
    	$id = input('id',0,'intval');
    	
    	$col = Db::name('column')->where('id',$cid)->find();
    	$this->assign('col',$col);
    	
    	$info = Db::name('article')->where(" isShow = 1 and id = $id ")->find();
    	
    	Db::name('article')->where(" isShow = 1 and id = $id ")->setInc('click');
    	
    	//上下文
    	$pre = Db::name('article')->where(" isShow = 1 and id < $id ")->order('id desc')->find();
    	$next = Db::name('article')->where(" isShow = 1 and id > $id ")->order('id asc')->find();
    	$info['pre'] = array('url'=>'javascript:void(0);','title'=>'没有了');
    	if (!empty($pre)){
    		$info['pre'] = array('url'=>url('index/detail',array('id'=>$pre['id'],'cid'=>$pre['cid'])),'title'=>$pre['title']);
    	}
    	$info['next'] = array('url'=>'javascript:void(0);','title'=>'没有了');
    	if (!empty($next)){
    		$info['next'] = array('url'=>url('index/detail',array('id'=>$next['id'],'cid'=>$next['cid'])),'title'=>$next['title']);
    	}
    	
    	$this->assign('info',$info);
    	return $this->fetch();
    }
    
    /**
     * 搜索
     */
    public function search(){

    	$keyword = input('keyword','','trim');
    	$this->assign('keyword',$keyword);
    	
    	$total = Db::name('article')->where(" isShow = 1 and title like '%".$keyword."%'")->count();
    	$this->assign('total',$total);
    	return $this->fetch();
    }
    
    /**
     * 文章列表
     */
    public function getArticles(){
    	
    	if ($this->request->isGet()){
    		
    		$page = input('page',1,'intval');
    		$isRecommend = input('isRecommend',-1,'intval');
    		$cid = input('cid',0,'intval');
    		$keyword = input('keyword','','trim');
    		
    		$where= " a.isShow = 1 ";
    		if ($isRecommend > -1){
    			$where .= " and isRecommend = ".$isRecommend;
    		}
    		if($cid > 0){
    			$where .= " and cid = ".$cid;
    		}
    		
    		if (!empty($keyword)){
    			$where .= " and a.title like '%".$keyword."%'";
    		}
    		
    		
    		$db = Db::name('article');
    		$lists = $db->alias('a')
	    		->where($where)
	    		->field('a.id,a.title,a.img,a.click,a.addTime,a.comment,a.cid,a.descript,b.title as ctitle')
	    		->join('__COLUMN__ b ',' a.cid = b.id','left')
	    		->order('addTime desc')
	    		->page($page,10)
	    		->select();
	    		foreach ($lists as $key => $v){
	    			$lists[$key]['addTime'] = date('Y-m-d H:i:s',$v['addTime']);
	    		}
    		ajax_return_ok($lists);
    	}
    	
    }
    
    
}
